#ifndef JSEDITOR_H
#define JSEDITOR_H

#include <QWidget>
#include <QFileSystemWatcher>
#include <client/dbclient.h>
#include "ui_ScriptEditor.h"
//#include "sqlparser/tosqlparse.h"
using namespace mongo;

class QTextDocument;
class QLabel;
class QProgressDialog;


class JsEditor : public QMainWindow
{
	Q_OBJECT

protected:
    DBClientConnection* clientConnection;
	public:
                JsEditor(QWidget * parent = 0);
                ~JsEditor(){}

		void saveOnExit();

		void setFileName(const QString & fname);
        QString fileName() { return m_fileName; }

		void setStatusMessage(const QString & message = 0);

        void setConnection(DBClientConnection* connect);
   	signals:

        void showJsResult(QString command);

        void jsScriptStart();

        void showJsScriptResult(QString line);

		void buildTree();

		void rebuildViewTree(QString schema, QString name);

	private:
        Ui::ScriptEditor ui;

		QString m_fileName;
		QFileSystemWatcher * m_fileWatcher;

		QLabel * changedLabel;
		QLabel * cursorLabel;
		QString cursorTemplate;

		bool canceled;
		bool m_scriptCancelled;

		QProgressDialog * progress;

		bool setProgress(int p);

		void showEvent(QShowEvent * event);
		bool changedConfirm();
		void saveFile();
		void open(const QString & newFile);

        QString query();

		void find(QString ttf, bool forward/*, bool backward*/);

		void setFileWatcher(const QString & newFileName);

	private slots:
        void action_Run_Js_triggered();
		void actionRun_Explain_triggered();
		void actionRun_as_Script_triggered();
		void action_Open_triggered();
		void action_Save_triggered();
		void action_New_triggered();
		void actionSave_As_triggered();
		void actionCreateView_triggered();
        void jsTextEdit_cursorPositionChanged(int,int);
		void documentChanged(bool state);
		void cancel();
		// searching
		void actionSearch_triggered();
		void searchEdit_textChanged(const QString & text);
		void findPrevious();
		void findNext();

		void externalFileChange(const QString & path);
		//
		void scriptCancelled();
};

#endif
